<template>
  <div class="app-container v-flex">
    <filter-form :form="listQuery" :data="filterData" only-query @search="search" @addForm="addForm">
      <el-button
        class="refresh-btn"
        size="mini"
        type="primary"
        icon="el-icon-refresh"
        @click="resetListQuery">重置</el-button>
      <el-button
        v-permission-button="publicConfig['ITPS-ADD']"
        size="mini"
        class="operation-btn"
        type="primary"
        icon="el-icon-plus"
        @click="addForm">新增</el-button>
    </filter-form>

    <!-- 表格 -->
    <div ref="main" class="v-flex-elastic">
      <base-table
        :height="this.height"
        :loading="tableData.loading"
        :data="tableData.tableList"
        :columns="tableData.columns"
        :show-index="tableData.showIndex"
        :show-selection="tableData.showSelection"
        :show-operation="tableData.showOperation"
        :show-filter-column="tableData.showFilterColumn"
        :operaton-width="tableData.operatonWidth"
        :default-operations="tableData.defaultOperations"
        :has-auth="tableData.hasAuth"
        :row-show-color="tableData.rowShowColor">
        <template slot="operations" slot-scope="{row, $index}">
          <el-button
            v-permission-button="publicConfig['ITPS-VIEW']"
            class="el-icon-defined"
            style="color: #009900;"
            size="mini"
            @click="view(row,$index)">查看</el-button>
          <el-button
            v-permission-button="(row.planStatus== 1 || row.planStatus== 4) && publicConfig['ITPS-UPDATE']"
            class="el-icon-defined"
            style="color: #FFFFFF;background-color: #FF9933;"
            size="mini"
            @click="updateForm(row,$index)">编辑</el-button>
          <el-button
            v-permission-button="(row.planStatus== 1 || row.planStatus== 4) && publicConfig['ITPS-DELETE']"
            class="el-icon-defined"
            style="color: #FFFFFF;background-color: #FF0000;"
            size="mini"
            @click="deleteData(row,$index)">删除</el-button>
          <el-button
            v-permission-button="(row.planStatus== 1 || row.planStatus== 4) && publicConfig['ITPS-SUBMIT']"
            class="el-icon-defined"
            style="color: #FFFFFF;background-color: #02A7F0;"
            size="mini"
            @click="submitData(row,$index)">提交</el-button>
          <el-button
            v-permission-button="row.planStatus == 2 && publicConfig['ITPS-CHECK']"
            style="color: #FFFFFF;background-color: #70B603;"
            class="el-icon-defined"
            size="mini"
            @click="checkData(row,$index)">审核</el-button>
        </template>
      </base-table>
    </div>
    <!-- 表格 -->

    <!-- 分页 -->
    <div class="page-box">
      <pagination
        :total="tableData.total"
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        @pagination="changePageOption" />
    </div>
    <!-- 分页 -->

    <!-- 新增或编辑弹框 -->
    <DataAddOrEdit :dialog="dialog" @updateData="updateData" />

    <!-- 查看弹框 -->
    <DataLook :dialog="dialog" :dialog-look="dialogLook" @updateCheckData="updateCheckData" />
  </div>
</template>
<script>
import { tableMixin } from '@/utils/public'
import { getUserInfo } from '@/utils/auth'
import { parseTime } from '@/utils'
import DataAddOrEdit from './components/DataAddOrEdit'
import DataLook from './components/DataLook'
import { getDeptAll } from '@/api/cms/basic'
import { getListPage, addOrEditData, deleteData } from '@/api/cms/Inspection-management/Inspection-plan-management/YearlyPlanManagement'
import { getDetailByPlanYearId } from '@/api/cms/Inspection-management/Inspection-plan-management/YearlyPlanDetail'
import { addOrEditAuditData } from '@/api/cms/Inspection-management/Inspection-plan-management/YearlyPlanAudit'
import { findManageUnitDeptList } from '@/api/platform'
import { getListByTypeCode } from '@/api/cms/basic'
export default {
  name: 'YearlyPlanManagement',
  components: {
    DataAddOrEdit,
    DataLook
  },
  mixins: [tableMixin],
  data() {
    return {
      deptAll: [],
      // 表格高度
      height: '100%',
      // 顶部操作栏配置
      filterData: {
        unitDeptCode: {
          clearable: true,
          key: 'unitDeptCode',
          type: 'select',
          title: '单位',
          placeholder: '请选择单位',
          options: [],
          props: { label: 'deptName', value: 'deptNo' }
        },
        // cType: {
        //   clearable: true,
        //   key: 'itemValue',
        //   type: 'select',
        //   title: '6C类型',
        //   placeholder: '请选择6C类型',
        //   options: [], // 装置类型,
        //   props: {
        //     label: 'itemName',
        //     value: 'itemValue'
        //   }
        // },
        planYear: {
          width: 'auto',
          key: 'planYear',
          type: 'date',
          dateType: 'year',
          valueFormat: 'yyyy',
          placeholder: '计划年份'
        }
      },
      // 查询列表请求参数
      listQuery: {
        unitDeptCode: null,
        cType: null,
        planYear: '',
        page: 1,
        size: 20
      },
      // 表格数组
      tableData: {
        loading: false,
        tableList: [],
        total: 0,
        columns: [
          { prop: 'deptName', label: '单位' },
          { prop: 'cType', label: '6C类型' },
          { prop: 'planYear', label: '计划年份', formatter: row => parseTime(row.planYear, '{y}') },
          { prop: 'preparedBy', label: '编制人' },
          { prop: 'checkBy', label: '审核人' },
          { prop: 'planStatus', label: '计划状态', formatter: (row) => (row.planStatus === '1' ? '编制中' : row.planStatus === '4' ? '不通过，待编制' : row.planStatus === '2' ? '已提交' : row.planStatus === '3' ? '已发布' : '') }
        ],
        showIndex: true,
        showSelection: false,
        showOperation: true,
        showFilterColumn: true,
        operatonWidth: '360px',
        defaultOperations: [],
        hasAuth: true,
        rowShowColor: { name: 'name', value: 0 }
      },
      // 添加/编辑弹框
      dialog: {
        title: '',
        visible: false,
        disabled: false,
        loading: false,
        isYearPlan: true, // 是否展示年计划保存按钮
        isMonthPlan: false, // 是否展示具体月计划保存按钮
        isAllPlan: false, // 该保存按钮为保存年计划和详情一起
        isYearlyPlanSave: false, // 年计划是否已保存，用于判断保存年计划详情
        isShowMonthPlan: false,
        yearlyPlanDetail: {
          planYearId: '',
          gt: {
            checked1: [],
            checked2: [],
            checked3: [],
            checked4: [],
            checked5: [],
            checked6: [],
            checked7: [],
            checked8: [],
            checked9: [],
            checked10: [],
            checked11: [],
            checked12: [],
            value1: [],
            value2: [],
            value3: [],
            value4: [],
            value5: [],
            value6: [],
            value7: [],
            value8: [],
            value9: [],
            value10: [],
            value11: [],
            value12: []
          },
          pt: {
            checked1: [],
            checked2: [],
            checked3: [],
            checked4: [],
            checked5: [],
            checked6: [],
            checked7: [],
            checked8: [],
            checked9: [],
            checked10: [],
            checked11: [],
            checked12: [],
            value1: [],
            value2: [],
            value3: [],
            value4: [],
            value5: [],
            value6: [],
            value7: [],
            value8: [],
            value9: [],
            value10: [],
            value11: [],
            value12: []
          }
        },
        dataList: {
          lineList: []
        },
        form: {},
        auditForm: {
          checkAdvice: '',
          checkBy: '',
          auditStatus: 3
        },
        detailForm: {},
        rules: {
          planYear: { required: true, message: '必填', trigger: 'blur' },
          cType: { required: true, message: '必填', trigger: 'blur' }
        }
      },
      // 查看弹框
      dialogLook: {
        visible: false
      }
    }
  },
  activated() {
    this.getListPage()
    this.getList()
  },
  created() {
    this.getListPage()
    this.getList()
    // getUserInfo()
  },
  mounted() {
  },
  methods: {
    deptFormatRow(param) {
      let strName = null
      this.deptAll.forEach(item => {
        if (param === item.deptId) {
          strName = item.deptName
        }
      })
      return strName
    },
    // 获取下拉数据、字典
    getList() {
      getListByTypeCode({ typeCode: 'JCJCTYPE' }).then(response => {
        this.filterData.cType.options = response.data.JCJCTYPE
      })
      getDeptAll().then(response => {
        this.deptAll = response.data
      })
      // 获取单位
      findManageUnitDeptList({}).then(response => {
        var resData = response.data
        for (var i in resData) {
          if (resData[i].deptNo === '001200') {
            resData.splice(i, 1)
          }
        }
        this.filterData.unitDeptCode.options = resData
      })
    },
    getListPage() {
      this.tableData.loading = true
      this.tableData.tableList = []
      this.tableData.total = 0
      getListPage(this.listQuery).then(response => {
        this.tableData.tableList = response.data.records
        this.tableData.total = response.data.total
        this.tableData.loading = false
      }).catch(() => {
        this.tableData.loading = false
      })
    },
    // 新增
    addForm() {
      const dialog = {
        title: '年计划基本信息添加',
        type: 'add',
        visible: true,
        disabled: false,
        loading: false,
        isYearPlan: true, // 是否展示年计划保存按钮
        isMonthPlan: false,
        isAllPlan: false, // 该保存按钮为保存年计划和详情一起
        isYearlyPlanSave: false, // 年计划是否已保存，用于判断保存年计划详情
        isShowMonthPlan: false,
        yearlyPlanDetail: {
          planYearId: '',
          gt: {
            checked1: [],
            checked2: [],
            checked3: [],
            checked4: [],
            checked5: [],
            checked6: [],
            checked7: [],
            checked8: [],
            checked9: [],
            checked10: [],
            checked11: [],
            checked12: [],
            value1: [],
            value2: [],
            value3: [],
            value4: [],
            value5: [],
            value6: [],
            value7: [],
            value8: [],
            value9: [],
            value10: [],
            value11: [],
            value12: []
          },
          pt: {
            checked1: [],
            checked2: [],
            checked3: [],
            checked4: [],
            checked5: [],
            checked6: [],
            checked7: [],
            checked8: [],
            checked9: [],
            checked10: [],
            checked11: [],
            checked12: [],
            value1: [],
            value2: [],
            value3: [],
            value4: [],
            value5: [],
            value6: [],
            value7: [],
            value8: [],
            value9: [],
            value10: [],
            value11: [],
            value12: []
          }
        },
        dataList: {
          lineList: this.dialog.dataList.lineList
        },
        form: {
          cType: '',
          planYear: null,
          planStatus: '1',
          preparedBy: getUserInfo().username,
          delFlag: 0
        },
        detailForm: {

        }
      }
      this.dialog = Object.assign(this.dialog, dialog)
    },
    // 编辑
    updateForm(row, $index) {
      this.currentRow = [row]
      const dialog = {
        title: '编辑',
        type: 'edit',
        visible: true,
        disabled: false,
        loading: false,
        isYearPlan: false, // 是否展示年计划保存按钮
        isMonthPlan: false,
        isAllPlan: true, // 该保存按钮为保存年计划和详情一起
        isYearlyPlanSave: true, // 年计划是否已保存，用于判断保存年计划详情
        isShowMonthPlan: true,
        yearlyPlanDetail: {
          planYearId: '',
          gt: {
            checked1: [],
            checked2: [],
            checked3: [],
            checked4: [],
            checked5: [],
            checked6: [],
            checked7: [],
            checked8: [],
            checked9: [],
            checked10: [],
            checked11: [],
            checked12: [],
            value1: [],
            value2: [],
            value3: [],
            value4: [],
            value5: [],
            value6: [],
            value7: [],
            value8: [],
            value9: [],
            value10: [],
            value11: [],
            value12: []
          },
          pt: {
            checked1: [],
            checked2: [],
            checked3: [],
            checked4: [],
            checked5: [],
            checked6: [],
            checked7: [],
            checked8: [],
            checked9: [],
            checked10: [],
            checked11: [],
            checked12: [],
            value1: [],
            value2: [],
            value3: [],
            value4: [],
            value5: [],
            value6: [],
            value7: [],
            value8: [],
            value9: [],
            value10: [],
            value11: [],
            value12: []
          }
        },
        dataList: {
          lineList: this.dialog.dataList.lineList
        },
        form: JSON.parse(JSON.stringify(row))
      }
      getDetailByPlanYearId({ 'planYearId': row.id }).then((response) => {
        response.data.forEach(item => {
          if (item.railwayType === 'GT') {
            switch (item.month) {
              case 1 :
                this.dialog.yearlyPlanDetail.gt.checked1.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.gt.value1.push(item.lineId)
                break // 停止执行，跳出switch
              case 2 :
                this.dialog.yearlyPlanDetail.gt.checked2.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.gt.value2.push(item.lineId)
                break // 停止执行，跳出switch
              case 3 :
                this.dialog.yearlyPlanDetail.gt.checked3.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.gt.value3.push(item.lineId)
                break // 停止执行，跳出switch
              case 4 :
                this.dialog.yearlyPlanDetail.gt.checked4.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.gt.value4.push(item.lineId)
                break // 停止执行，跳出switch
              case 5 :
                this.dialog.yearlyPlanDetail.gt.checked5.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.gt.value5.push(item.lineId)
                break // 停止执行，跳出switch
              case 6 :
                this.dialog.yearlyPlanDetail.gt.checked6.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.gt.value6.push(item.lineId)
                break // 停止执行，跳出switch
              case 7 :
                this.dialog.yearlyPlanDetail.gt.checked7.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.gt.value7.push(item.lineId)
                break // 停止执行，跳出switch
              case 8 :
                this.dialog.yearlyPlanDetail.gt.checked8.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.gt.value8.push(item.lineId)
                break // 停止执行，跳出switch
              case 9 :
                this.dialog.yearlyPlanDetail.gt.checked9.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.gt.value9.push(item.lineId)
                break // 停止执行，跳出switch
              case 10 :
                this.dialog.yearlyPlanDetail.gt.checked10.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.gt.value10.push(item.lineId)
                break // 停止执行，跳出switch
              case 11 :
                this.dialog.yearlyPlanDetail.gt.checked11.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.gt.value11.push(item.lineId)
                break // 停止执行，跳出switch
              case 12 :
                this.dialog.yearlyPlanDetail.gt.checked12.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.gt.value12.push(item.lineId)
                break // 停止执行，跳出switch
              default : // 上述条件都不满足时，默认执行的代码
                console.log(item.month)
            }
          } else {
            switch (item.month) {
              case 1 :
                this.dialog.yearlyPlanDetail.pt.checked1.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.pt.value1.push(item.lineId)
                break // 停止执行，跳出switch
              case 2 :
                this.dialog.yearlyPlanDetail.pt.checked2.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.pt.value2.push(item.lineId)
                break // 停止执行，跳出switch
              case 3 :
                this.dialog.yearlyPlanDetail.pt.checked3.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.pt.value3.push(item.lineId)
                break // 停止执行，跳出switch
              case 4 :
                this.dialog.yearlyPlanDetail.pt.checked4.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.pt.value4.push(item.lineId)
                break // 停止执行，跳出switch
              case 5 :
                this.dialog.yearlyPlanDetail.pt.checked5.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.pt.value5.push(item.lineId)
                break // 停止执行，跳出switch
              case 6 :
                this.dialog.yearlyPlanDetail.pt.checked6.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.pt.value6.push(item.lineId)
                break // 停止执行，跳出switch
              case 7 :
                this.dialog.yearlyPlanDetail.pt.checked7.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.pt.value7.push(item.lineId)
                break // 停止执行，跳出switch
              case 8 :
                this.dialog.yearlyPlanDetail.pt.checked8.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.pt.value8.push(item.lineId)
                break // 停止执行，跳出switch
              case 9 :
                this.dialog.yearlyPlanDetail.pt.checked9.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.pt.value9.push(item.lineId)
                break // 停止执行，跳出switch
              case 10 :
                this.dialog.yearlyPlanDetail.pt.checked10.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.pt.value10.push(item.lineId)
                break // 停止执行，跳出switch
              case 11 :
                this.dialog.yearlyPlanDetail.pt.checked11.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.pt.value11.push(item.lineId)
                break // 停止执行，跳出switch
              case 12 :
                this.dialog.yearlyPlanDetail.pt.checked12.push({ lineId: item.lineId, exeNum: item.executeNum })
                this.dialog.yearlyPlanDetail.pt.value12.push(item.lineId)
                break // 停止执行，跳出switch
              default : // 上述条件都不满足时，默认执行的代码
                console.log(item.month)
            }
          }
        })
      })
      this.dialog = Object.assign(this.dialog, dialog)
    },
    view(row, $index) { // 查看
      this.currentRow = [row]
      const dialog = {
        title: '查看',
        type: 'look',
        visible: false,
        disabled: true,
        loading: false,
        isYearPlan: false, // 是否展示年计划保存按钮
        isMonthPlan: true,
        isAllPlan: false, // 该保存按钮为保存年计划和详情一起
        isYearlyPlanSave: true, // 年计划是否已保存，用于判断保存年计划详情
        isShowMonthPlan: true,
        auditForm: {
          checkAdvice: '',
          checkBy: '',
          auditStatus: 3
        },
        yearlyPlanDetail: {
          planYearId: '',
          gt: {
            checked1: [],
            checked2: [],
            checked3: [],
            checked4: [],
            checked5: [],
            checked6: [],
            checked7: [],
            checked8: [],
            checked9: [],
            checked10: [],
            checked11: [],
            checked12: [],
            value1: [],
            value2: [],
            value3: [],
            value4: [],
            value5: [],
            value6: [],
            value7: [],
            value8: [],
            value9: [],
            value10: [],
            value11: [],
            value12: []
          },
          pt: {
            checked1: [],
            checked2: [],
            checked3: [],
            checked4: [],
            checked5: [],
            checked6: [],
            checked7: [],
            checked8: [],
            checked9: [],
            checked10: [],
            checked11: [],
            checked12: [],
            value1: [],
            value2: [],
            value3: [],
            value4: [],
            value5: [],
            value6: [],
            value7: [],
            value8: [],
            value9: [],
            value10: [],
            value11: [],
            value12: []
          }
        },
        form: JSON.parse(JSON.stringify(row))
      }
      this.dialog = Object.assign(this.dialog, dialog)
      this.dialogLook.visible = true
    },
    checkData(row, $index) { // 查看
      this.currentRow = [row]
      const dialog = {
        title: '审核',
        type: 'check',
        visible: false,
        disabled: false,
        loading: false,
        isYearPlan: false, // 是否展示年计划保存按钮
        isMonthPlan: true,
        isShowMonthPlan: true,
        isAllPlan: false, // 该保存按钮为保存年计划和详情一起
        isYearlyPlanSave: true, // 年计划是否已保存，用于判断保存年计划详情
        auditForm: {
          checkAdvice: '',
          checkBy: '',
          auditStatus: 3
        },
        yearlyPlanDetail: {
          planYearId: '',
          gt: {
            checked1: [],
            checked2: [],
            checked3: [],
            checked4: [],
            checked5: [],
            checked6: [],
            checked7: [],
            checked8: [],
            checked9: [],
            checked10: [],
            checked11: [],
            checked12: [],
            value1: [],
            value2: [],
            value3: [],
            value4: [],
            value5: [],
            value6: [],
            value7: [],
            value8: [],
            value9: [],
            value10: [],
            value11: [],
            value12: []
          },
          pt: {
            checked1: [],
            checked2: [],
            checked3: [],
            checked4: [],
            checked5: [],
            checked6: [],
            checked7: [],
            checked8: [],
            checked9: [],
            checked10: [],
            checked11: [],
            checked12: [],
            value1: [],
            value2: [],
            value3: [],
            value4: [],
            value5: [],
            value6: [],
            value7: [],
            value8: [],
            value9: [],
            value10: [],
            value11: [],
            value12: []
          }
        },
        form: JSON.parse(JSON.stringify(row))
      }
      this.dialog = Object.assign(this.dialog, dialog)
      this.dialogLook.visible = true
    },
    // 查询
    search() {
      this.listQuery.page = 1
      this.getListPage()
    },
    // 新增或编辑弹框$emit 回调方法
    updateData(formData) {
      const param = Object.assign({}, formData)
      addOrEditData(param)
        .then((response) => {
          this.currentRow = [response.data]
          if (this.dialog.type === 'add') {
            this.tableData.tableList.unshift(this.currentRow[0])
            this.tableData.total++
            this.notifiySuccess('添加成功')
            var dialog = this.dialog
            dialog.form.id = response.data.id
            this.dialog = Object.assign(this.dialog, dialog)
          } else {
            const index = this.tableData.tableList.findIndex(
              (item) => item.id === this.currentRow[0].id
            )
            this.tableData.tableList.splice(
              index,
              1,
              JSON.parse(JSON.stringify(this.currentRow[0]))
            )
            this.$forceUpdate()
            this.notifiySuccess('保存成功')
          }
          this.dialog.visible = false
          this.dialog.loading = false
          this.dialog.isMonthPlan = true
          this.dialog.isShowMonthPlan = true
          this.dialog.isYearPlan = false
          this.dialog.isAllPlan = false
          this.resetListQuery()
        })
        .catch((_) => {
          this.dialog.loading = false
        })
    },
    // 审核后调函数
    updateCheckData(formData) {
      this.getListPage()
    },
    // 删除
    deleteData(row, $index) {
      this.confirmBox(`确定删除这条数据?`)
        .then(() => {
          deleteData(row.id).then(response => {
            this.notifiySuccess('删除成功!')
            this.getListPage()
          })
        })
        .catch(() => {})
    },
    submitData(row, $index) {
      this.confirmBox(`确定提交这条数据?`)
        .then(() => {
          addOrEditAuditData({ 'planYearId': row.id, 'auditStatus': 2 }).then(response => {
            this.notifiySuccess('提交成功')
            this.getListPage()
          })
        })
        .catch(() => {})
    },
    // 改变分页配置
    changePageOption(data) {
      this.listQuery.page = data.page
      this.listQuery.size = data.size
      this.getListPage()
    },
    // 重置查询
    resetListQuery() {
      this.listQuery = {
        page: 1,
        size: 20
      }
      this.getListPage()
    }
  }
}
</script>

